﻿using System;
using System.Data;
using System.Web;
using Soveryn.Commerce.API;
using Soveryn.Commerce.BaseClasses;
using Soveryn.Commerce.Shared;

namespace Soveryn.Commerce.Controls
{
    public partial class EditModeToolbar : ControlBaseClass
    {

        protected void Page_Load(object sender, EventArgs e)
        {

            if (IsPostBack)
                return;

            if (ParentPage.EditMode)
            {
                DoodadModeButton.CssClass = "cmsButton cmsSmall cmsButtonBlue active";
                InlineModeButton.CssClass = "cmsButton cmsSmall cmsButtonBlue";
            }
            else
            {
                DoodadModeButton.CssClass = "cmsButton cmsSmall cmsButtonBlue";
                InlineModeButton.CssClass = "cmsButton cmsSmall cmsButtonBlue active";
            }

            if (ParentPage.EditMode && ParentPage.EmployeeLoggedIn)
            {
                SavePageButton.Visible = true;
                SavePageButton.Attributes["onclick"] = "cmsSaveAloha('" + ParentPage.EmployeeToken + "','" + PageBaseClass.WebsiteToken + "', this);$('#cmsEditModeShield').css('display', 'block');";
            }

            //ManageDoodadsHyperLink.Attributes["onclick"] = "cmsManageDoodads('" + this.ParentPage.PageID + "');";

            ManagePageHyperLink.Attributes["onclick"] = "cmsManagePage('" + ParentPage.EmployeeToken + "','" + PageBaseClass.WebsiteToken + "','" + this.ParentPage.PageID + "');";

            PageTitleLiteral.Text = ParentPage.Properties.SEO.HeadTitle;

            if (ParentPage.Properties.TemplatePageID.Value != 0)
            {
                PageTemplatesLI.Visible = true;
                //TemplatePageHyperlink.NavigateUrl = "/Page.aspx?PageID=" + ParentPage.Properties.TemplatePageID;

                DataSet pageTemplates =  WebsiteData.PageTemplateListForPage(this.ParentPage.PageID);
                if (DataUtils.HasRecords(pageTemplates,0))
                {
                    PageTemplatesLiteral.Text += "<ul>";
                    foreach (DataRow row in pageTemplates.Tables[0].Rows)
                    {
                        PageProperties page = PageProperties.Create(row);
                        PageTemplatesLiteral.Text += "<li><a href='/Page.aspx?PageID=" + page.PageID + "'>" + page.SEO.HeadTitle + "</a></li>";
                    }
                    PageTemplatesLiteral.Text += "</ul>";
                }
            }

        }

        protected void ExitButton_Click(object sender, EventArgs e)
        {

            Response.Cookies.Add(new HttpCookie("EmployeeToken", "false"));
            ParentPage.SetEditMode(false);
            Response.Redirect(Request.RawUrl.Replace("EmployeeToken", "OldEmployeeToken"), true);

        }

        protected void RebuildCacheButton_Click(object sender, EventArgs e)
        {
            CacheUtils.ClearCache();
            ParentPage.Reload();
        }

        protected void InlineModeButton_Click(object sender, EventArgs e)
        {
            ParentPage.SetEditMode(false);
            ParentPage.Reload();
        }
        protected void DoodadModeButton_Click(object sender, EventArgs e)
        {
            ParentPage.SetEditMode(true);
            ParentPage.Reload();
        }
}
}